<template>
  <div>
	  <el-button @click="goBack()">返回主页</el-button><el-button @click="goback()">返回上一页</el-button>
    <div>我要提问</div>
    <div class="chat-container">
      <div class="input-container">
        <el-input v-model="input" placeholder="请输入问题..." />
        <el-button @click="sendFeedback()">发送</el-button>
        <el-button @click="checkFeedback()">查看历史记录</el-button>
      </div>
    </div>


    <!-- 显示已反馈列表 -->
    <div v-if="feedbacks.length > 0">
      <h3>反馈列表：</h3>
      <el-table :data="feedbacks" style="width: 100%">
        <el-table-column prop="content" label="问题"></el-table-column>
        <el-table-column prop="feedback" label="反馈"></el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { usePersonStore } from '../store/person.js';
import { getQue, checkQue } from '../utils/requestApi.js';
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()

const input = ref('');
const sentQuestions = ref([]); // 存储已发送的问题
const feedbacks = ref([]); // 存储从后端获取的反馈列表

const { personinfo } = usePersonStore(); // 这里仅引入 personinfo，不修改它

const token = localStorage.token;

const sendFeedback = async () => {
  const feedbackData = {
    token,
    content: input.value
  };

  try {
    const response = await getQue(feedbackData);
    if (response.data.code === "200") {
      ElMessage({
        message: '提问成功',
        type: 'success'
      });
      sentQuestions.value.push(input.value);
      input.value = ''; // 清空输入框内容
    } else {
      ElMessage({
        message: '错误',
        type: 'danger'
      });
    }
  } catch (error) {
    ElMessage({
      message: '错误：' + error.message,
      type: 'danger'
    });
  }
};

const checkFeedback = async () => {
  const checkfeedbackData1 = {
    token,
    status: '0'
  };
  
  const checkfeedbackData2 = {
    token,
    status: '1'
  };

  try {
    // 发送第一个请求并处理反馈
    const response1 = await checkQue(checkfeedbackData2);
    if (response1.data.code === "200") {
      const feedbackList1 = response1.data.data;
      feedbacks.value = feedbackList1;
      feedbackList1.forEach(feedback => {
        sentQuestions.value.push(feedback.content,feedback.feedback);
      });
      ElMessage({
        message: '查询成功',
        type: 'success'
      });
    } else {
      ElMessage({
        message: '错误',
        type: 'danger'
      });
    }

    // 发送第二个请求并处理反馈
    const response2 = await checkQue(checkfeedbackData1);
    if (response2.data.code === "200") {
      const feedbackList2 = response2.data.data;
      feedbackList2.forEach(feedback => {
        sentQuestions.value.push(feedback.content);
      });
      ElMessage({
        message: '查询成功',
        type: 'success'
      });
    } else {
      ElMessage({
        message: '错误',
        type: 'danger'
      });
    }
  } catch (error) {
    ElMessage({
      message: '错误：' + error.message,
      type: 'danger'
    });
  }
};

const goBack = () => {
  router.push({path: '/home'});
};
const goback = () => {
  router.push({path: '/feedback'});
};
</script>
